<?php
namespace app\crm\model;
use think\Db;
use think\Model;
use think\Cache;
use think\Loader;

class Target extends Model
{
    public function userDailyData () 
    {
        $page = $_REQUEST["page"]; // 页码
        $limit = $_REQUEST["limit"]; // 每页显示条数
        $count = Db::name('sys_daily_target')->count();
        // 查询状态为1的用户数据 并且每页显示10条数据
        $targetDataList = Db::name('sys_daily_target')->limit(($page-1)*$limit,$limit)->order('createtime DESC')->select();
        $users = Cache::get('users');
        foreach ($targetDataList as &$val) {
            $val['username'] = $users[$val['username']]['cn_name'];
        }
        $rowsList = count($targetDataList) ? $targetDataList : false;
        $arrData["code"] = 0;
        $arrData["msg"] = '';
        $arrData["count"] = $count;
        $arrData["data"] = $rowsList;
        return $arrData;
    }


    public function userMonthlyData () 
    {
        $page = $_REQUEST["page"]; // 页码
        $limit = $_REQUEST["limit"]; // 每页显示条数
        $count = Db::name('sys_monthly_target')->count();
        // 查询状态为1的用户数据 并且每页显示10条数据
        $targetDataList = Db::name('sys_monthly_target')->limit(($page-1)*$limit,$limit)->order('createtime DESC')->select();
        $users = Cache::get('users');
        foreach ($targetDataList as &$val) {
            $val['username'] = $users[$val['username']]['cn_name'];
        }
        $rowsList = count($targetDataList) ? $targetDataList : false;
        $arrData["code"] = 0;
        $arrData["msg"] = '';
        $arrData["count"] = $count;
        $arrData["data"] = $rowsList;
        return $arrData;
    }


    public function delDailyTarget ($id)
    {
        $result = Db::name('sys_daily_target')->delete($id);
        if ($result) {
            echo json_encode(array('success'=>true,'msg'=>'删除成功！'));
        } else {
            echo json_encode(array('msg'=>'删除失败！'));
        }
    }


    public function delMonthlyTarget ($id)
    {
        $result = Db::name('sys_monthly_target')->delete($id);
        if ($result) {
            echo json_encode(array('success'=>true,'msg'=>'删除成功！'));
        } else {
            echo json_encode(array('msg'=>'删除失败！'));
        }
    }

}